package com.dingding.special.user.set.dao.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.beans.special.SpecialOutwardrunrecord;
import com.dingding.common.core.beans.special.Specialuserposition;
import com.dingding.common.core.util.bean.BeanUtilx;
import com.dingding.facade.special.trasfer.up_posi.ReqSetUserStatusDTO;
import com.dingding.special.user.set.dao.ISetUserStatusDao;

/**
 * 
 * @ClassName: ISetUserStatusDao 
 * @Description: 设置用户当前状态操作DAO
 * @author CHEHAUNBO 
 * @date 2016年8月19日 上午10:25:20
 * @since V2.4.0
 * 
 */
@Component("setUserStatusDao")
public class SetUserStatusDaoImpl implements ISetUserStatusDao {

	@Resource(name = "criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;

	private static Logger log = Logger.getLogger(SetUserStatusDaoImpl.class);

	/**
	 * 通过用户ID,获取用户实时位置
	 * 
	 * @Title: getSpecialUserPositionByUserId
	 * @param userId 用户ID
	 * @return Specialuserposition 
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	@Transactional(readOnly = true, isolation = Isolation.READ_COMMITTED)
	public Specialuserposition getSpecialDriverPositionByUserId(String userId) {

		try {
			return criteriaBaseDao.queryCriteriaListUnique(Specialuserposition.class,
					Restrictions.eq("userId", userId));
		} catch (Exception e) {
			log.error("通过用户ID,获取用户实时位置数据出现异常！", e);
			return null;
		}

	}

	/**
	 * 
	 * 保存用户实时位置（车主）
	 * 
	 * @Title: saveSpecialDriverPosition
	 * @return true 成功 false 失败
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public boolean saveSpecialDriverPosition(Specialuserposition specialuserposition) {

		try {

			criteriaBaseDao.save(specialuserposition);

			return true;

		} catch (Exception e) {
			log.error("保存用户实时位置出现异常！", e);
		}

		return false;

	}

	/**
	 * 
	 * 保存用户实时位置（车主）
	 * 
	 * @Title: saveSpecialDriverPosition
	 * @return true 成功 false 失败
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public boolean saveSpecialDriverPosition(ReqSetUserStatusDTO reqSetUserStatusDTO) {

		try {

			Specialuserposition specialuserposition = new Specialuserposition();

			specialuserposition.setSupId(UUID.randomUUID().toString()); // 主键ID
			specialuserposition.setOutwardRunTime(new Date());// 出车时间
			specialuserposition.setSupStatus(1); // 用户状态
			specialuserposition.setOnlineDuration(new BigDecimal(0));

			criteriaBaseDao.save(specialuserposition);

			return true;

		} catch (Exception e) {
			log.error("保存用户实时位置出现异常！", e);
		}

		return false;

	}

	/**
	 * 
	 * 保存用户实时位置（车主）
	 * 
	 * @Title: saveSpecialDriverPosition
	 * @return true 成功 false 失败
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	public boolean updateSpecialDriverPosition(Specialuserposition specialuserposition, int type) {

		try {

			criteriaBaseDao.saveOrUpdate(specialuserposition);

			return true;

		} catch (Exception e) {
			log.error("保存用户实时位置出现异常！", e);
		}

		return false;

	}

	/**
	 * 
	 * 保存用户出车记录信息
	 * 
	 * @Title: saveOutwardRunRecord 
	 * @param specialuserposition
	 * @param type  操作类型  出车 or 收车
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	public void saveOutwardRunRecord(Specialuserposition specialuserposition, int type) {

		SpecialOutwardrunrecord outwardrunrecord = new SpecialOutwardrunrecord();
		outwardrunrecord.setOrrId(UUID.randomUUID().toString());
		outwardrunrecord.setCreateTime(new Date());
		outwardrunrecord.setType(type);
		BeanUtilx.copyProperties(outwardrunrecord, specialuserposition);

		try {
			criteriaBaseDao.save(outwardrunrecord);
		} catch (Exception e) {
			log.error("保存用户出车记录信息出现异常！", e);
		}

	}

}
